package lightswitch.server.code.sqlhelper.expression.arithmetic;

import java.util.List;

import lightswitch.server.code.object.DataRow;
import lightswitch.server.code.sqlhelper.ExpressionHelper;
import lightswitch.server.code.sqlhelper.expression.BinaryExpression;
import lightswitch.server.code.sqlhelper.expression.Expression;
import lightswitch.server.code.sqlhelper.statement.select.From;
import lightswitch.server.code.util.TypeOfData;

public class Addition extends BinaryExpression {

	public Addition(Expression left, Expression right, boolean isNot) {
		this.leftExpression = left;
		this.rightExpression = right;
		this.isNot = isNot;
	}

	public Addition(String left, String right, boolean isNot, From _from) {
		leftExpression = ExpressionHelper.parseExpression(left,_from);
		rightExpression = ExpressionHelper.parseExpression(right,_from);
		this.isNot = isNot;
	}

	public String toString() {
		return "(" + leftExpression.toString() + " + "
				+ rightExpression.toString();
	}

	public Object GetValue(DataRow row, List<From> parentFromList){
		Object left = leftExpression.GetValue(row,parentFromList);
		Object right = rightExpression.GetValue(row,parentFromList);

		String type1 = leftExpression.dataType;
		String type2 = rightExpression.dataType;

		String type = "";
		if (type1.equals(type2)) {
			type = type1;

		} else {
			if ((type1.equals(TypeOfData.INT) && type2.equals(TypeOfData.FLOAT))
					|| (type1.equals(TypeOfData.INT) && type2.equals(TypeOfData.FLOAT))) {
				type = TypeOfData.FLOAT;

			}  else if ((type1.equals(TypeOfData.CHAR) || type2.equals(TypeOfData.CHAR))) {
				type = TypeOfData.CHAR;

			} else {
				type = TypeOfData.BOOLEAN;
			}
		}
		
		switch(type)
		{
		case "int":
			dataType = TypeOfData.INT;
				return Integer.parseInt(left.toString()) + Integer.parseInt(right.toString());
		case "float":
			dataType=TypeOfData.FLOAT;
			return Float.parseFloat(left.toString()) * Float.parseFloat(right.toString());
		case "char":
			dataType = TypeOfData.CHAR;
			return left.toString() + right.toString();
		default:

			break;
		}
		return null;
	}

}
